using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class SelectCreateItemItem : BaseItem
	{
		private int selectIndex;

		private Transform displayer;

		private Transform btnConfirm;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			displayer = model.Find("Displayer");
			displayer.gameObject.layer = 15;
			btnConfirm = model.Find("BtnConfirm");
			btnConfirm.gameObject.layer = 15;
			SetDisplayerImage(data.state);
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (!(raycastHit2D.collider != null) || !(raycastHit2D.collider.transform == btnConfirm))
			{
				return;
			}
			SetBtnState(btnConfirm, true);
			if (archiveData.createIndexs.Count >= base.itemData.createAmount)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_error");
				return;
			}
			Transform child = displayer.GetChild(selectIndex);
			if (child != null)
			{
				string id = child.transform.name;
				Transform transform = model.Find("CreatePoint");
				BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(id, transform.position, 0);
				if (baseItem != null)
				{
					AddCreateItem(baseItem);
					baseItem.DropDown(baseItem.transform.position);
					Singleton<AudioManager>.Instance.PlaySound("sound_explode");
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", baseItem.transform.position + new Vector3(0f, 0.4f, -0.0004f), 2f);
					Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", base.transform.position + new Vector3(0f, 0.4f, -0.0004f), 3f);
				}
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			SetBtnState(btnConfirm, false);
		}

		private void SetBtnState(Transform trans, bool isDown)
		{
			trans.Find("Down").SetObjectActive(isDown);
			trans.Find("Up").SetObjectActive(!isDown);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null && raycastHit2D.collider.transform == displayer)
			{
				SetDisplayerImage(selectIndex + 1);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
		}

		private void SetDisplayerImage(int index)
		{
			if (displayer.childCount == 0 || selectIndex == index)
			{
				return;
			}
			selectIndex = index;
			if (selectIndex >= displayer.childCount)
			{
				selectIndex = 0;
			}
			for (int i = 0; i < displayer.childCount; i++)
			{
				if (selectIndex == i)
				{
					displayer.GetChild(i).gameObject.SetActive(true);
				}
				else
				{
					displayer.GetChild(i).gameObject.SetActive(false);
				}
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = selectIndex;
		}
	}
}
